<?php
class Admin_ProductController extends Zendvn_Controller_Action {

  // Mang tham so nhan duoc o moi Action
  protected $_arrParam;

  // Duong dan cua Controller
  protected $_currentController;

  // Duong dan cua Action chinh
  protected $_actionMain;

  protected $_paginator = array('itemCountPerPage' => 10, 'pageRange' => 3);

  protected $_namespace;

  public function init() {
    parent::init();
    // Mang tham so nhan duoc o moi Action
    $this->_arrParam = $this->_request->getParams();

    // Duong dan cua Controller
    $this->_currentController = '/' . $this->_arrParam['module'] . '/' . $this->_arrParam['controller'];

    // Duong dan cua Action chinh
    $this->_actionMain = '/' . $this->_arrParam['module'] . '/' . $this->_arrParam['controller'] . '/index';

    $this->_paginator['currentPage'] = $this->_request->getParam('page', 1);
    $this->_arrParam['paginator'] = $this->_paginator;

    $this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
    $ssFilter = new Zend_Session_Namespace($this->_namespace);

    if (empty($ssFilter->col)) {
      $ssFilter->keywords = '';
      $ssFilter->col = 'p.id';
      $ssFilter->order = 'ASC';
      $ssFilter->cat_id = 0;
    }

    $this->_arrParam['ssFilter']['keywords'] = $ssFilter->keywords;
    $this->_arrParam['ssFilter']['col'] = $ssFilter->col;
    $this->_arrParam['ssFilter']['order'] = $ssFilter->order;
    $this->_arrParam['ssFilter']['cat_id'] = $ssFilter->cat_id;

    $this->view->arrParam = $this->_arrParam;
    $this->view->currentController = $this->_currentController;
    $this->view->actionMain = $this->_actionMain;

    $template_path = TEMPLATE_PATH . "/admin/system";
    $this->loadTemplate($template_path, 'template.ini', 'template');
  }

  public function indexAction() {
    $this->view->Title = "Product Management :: List";
    $this->view->headTitle($this->view->Title, true);

    $tblCategory = new Admin_Model_Category();
    $this->view->slbCategory = $tblCategory->itemInSelectbox();
    $tblProduct = new Admin_Model_Product();
    $this->view->Items = $tblProduct->listItem($this->_arrParam, array('task' => 'product-list'));

    $totalItem = $tblProduct->countItem($this->_arrParam);
    $paginator = new Zendvn_Paginator();
    $this->view->paginator = $paginator->createPaginator($totalItem, $this->_paginator);
  }

  public function addAction() {
    $this->view->Title = "Product Management :: Add";
    $this->view->headTitle($this->view->Title, true);

    $tblCategory = new Admin_Model_Category();
    $this->view->slbCategory = $tblCategory->itemInSelectbox();

    if ($this->_request->isPost()) {
      $validator = new Admin_Form_ValidateProduct($this->_arrParam);
      if ($validator->isError() == true) {
        $this->view->messageError = $validator->getMessageError();
        $this->view->Item = $validator->getData();
      } else {
        $tblProduct = new Admin_Model_Product();
        $arrParam = $validator->getData(array('upload' => true));
        $tblProduct->saveItem($arrParam, array('task' => 'product-add'));
        $this->_redirect($this->_actionMain);
      }
    }
  }

  public function filterAction() {
    $ssFilter = new Zend_Session_Namespace($this->_namespace);
    if ($this->_arrParam['type'] == 'search') {
      if ($this->_arrParam['key'] == 1) {
        $ssFilter->keywords = trim($this->_arrParam['keywords']);
      } else {
        $ssFilter->keywords = '';
      }
    }
    if ($this->_arrParam['type'] == 'order') {
      $ssFilter->col = $this->_arrParam['col'];
      $ssFilter->order = $this->_arrParam['by'];
    }
    if ($this->_arrParam['type'] == 'group') {
      $ssFilter->group_id = $this->_arrParam['group_id'];
    }
    $this->_redirect($this->_actionMain);
    $this->_helper->viewRenderer->setNoRender();
  }

  public function statusAction() {
    $tblProduct = new Admin_Model_Product();
    $tblProduct->changeStatus($this->_arrParam, null);
    $this->_redirect($this->_actionMain);
    $this->_helper->viewRenderer->setNoRender();
  }

  public function deleteAction() {
    $this->view->Title = 'Product Management :: Delete';
    $this->view->headTitle($this->view->Title, true);
    if ($this->_request->isPost()) {
      $tblProduct = new Admin_Model_Product();
      $tblProduct->deleteItem($this->_arrParam, array('task' => 'product-delete'));
      $this->_redirect($this->_actionMain);
    }
  }

  public function multiDeleteAction() {

    if ($this->_request->isPost()) {
      $tblProduct = new Admin_Model_Product();
      $tblProduct->deleteItem($this->_arrParam, array('task' => 'product-multi-delete'));
      $this->_redirect($this->_actionMain);
    }
    $this->_helper->viewRenderer->setNoRender();
  }

  public function editAction() {
    $this->view->Title = 'Product Management :: Edit';
    $this->view->headTitle($this->view->Title, true);

    $tblCategory = new Admin_Model_Category();
    $this->view->slbCategory = $tblCategory->itemInSelectbox();

    $tblProduct = new Admin_Model_Product();
    $this->view->Item = $tblProduct->getItem($this->_arrParam, array('task' => 'product-info'));

    if ($this->_request->isPost()) {
      $validator = new Admin_Form_ValidateProduct($this->_arrParam);
      if ($validator->isError() == true) {
        $this->view->messageError = $validator->getMessageError();
        $this->view->Item = $validator->getData();
      } else {
        $tblProduct = new Admin_Model_Product();
        $arrParam = $validator->getData(array('upload' => true));
        $tblProduct->saveItem($arrParam, array('task' => 'product-edit'));
        $this->_redirect($this->_actionMain);
      }
    }
  }

  public function infoAction() {
    $this->view->Title = 'Product Management :: Info';
    $this->view->headTitle($this->view->Title, true);
    $tblUser = new Admin_Model_Product();
    $this->view->Item = $tblUser->getItem($this->_arrParam, array('task' => 'product-info'));
  }

  public function sortAction() {
    if ($this->_request->isPost()) {
      $tblProduct = new Admin_Model_Product();
      $tblProduct->sortItem($this->_arrParam);
      $this->_redirect($this->_actionMain);
    }
    $this->_helper->viewRenderer->setNoRender();
  }
}
